<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/31
 * Time: 14:02
 */

namespace app\admin\controller;


use think\Db;
use think\Loader;

class Links extends Base
{
    /*友情链接列表*/
    public function ls(){
        $link = new \data\model\Links();
        $res = $link->paginate(10);
        $this->assign('res',$res);
        return $this->fetch('admin/link/ls');
    }
    /*添加友情链接*/
    public function add(){
        if(request()->post()){
            $data=[
                'lkname' =>input('lkname'),
                'lkurl' =>input('lkurl')
            ];
            $link = new \data\model\Links();
            $rs = $link->validate('links')->save($data);
            if(false === $rs){
                $this->error($link->getError());die();
            }
            if($rs){
                $this->success('添加链接成功','ls');
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch('admin/link/add');
    }
    /*修改友情链接*/
    public function edit(){
        $id = input('id');
        $lk = \data\model\Links::get(['lkid'=>$id]);
        $this->assign('lk',$lk);
        if(request()->post()){
            $data=[
                'lkid'=>input('id'),
                'lkname'=>input('lkname'),
                'lkurl'=>input('lkurl'),
            ];
            $lk = new \data\model\Links();
            $rs = $lk->validate('links')->isUpdate(true)->save($data);
            if(false === $rs){
                $this->error($lk->getError());die();
            }
            if($rs){
                $this->success('修改链接成功','ls');
            }else{
                $this->error('修改链接失败');
            }
        }
        return $this->fetch('admin/link/edit');
    }
    /*删除友情链接*/
    public  function del(){
        if(\data\model\Links::destroy(input('id'))){
            $this->success('删除链接成功','ls');
        }else{
            $this->error('删除链接失败');
        }
    }
}